import {testType} from "./common";

export function reg(patternName,oldValue){
    var flag,pattern,decorate,reg,tip;
    switch(patternName){
        case "any":
            tip="any input"
            flag={valid:true,tip:tip};
            break;
        case "Boolean":
            tip="only boolean is accept"
            if(oldValue==='true'||oldValue==='false'||testType(oldValue)==='Boolean'){
                flag={valid:true,tip:tip};
            }else{
                flag={valid:false,tip:tip};
            };
            break;
        case "Num":
            pattern="^[0-9]*$";
            tip="only number is accept"
            reg=new RegExp(pattern);
            flag={valid:reg.test(oldValue),tip:tip};
            break;
        case "email":
            pattern=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            tip="only email is accept"
            reg=new RegExp(pattern);
            flag={valid:reg.test(oldValue),tip:tip};
            break;
        case "url":
            pattern=/^((https|http|ftp|rtsp|mms){0,1}(:\/\/){0,1})www\.(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/i;
            tip="only url is accept"
            reg=new RegExp(pattern);
            flag={valid:reg.test(oldValue),tip:tip};
            break;
        case "EN"://英文
            pattern="^[A-Za-z]+$";
            tip="only english or number is accept"
            reg=new RegExp(pattern);
            flag={valid:reg.test(oldValue),tip:tip};
            break;
        case "ENNum"://英文和数字
            pattern="^[A-Za-z0-9]+$";
            tip="only english or number is accept"
            reg=new RegExp(pattern);
            flag={valid:reg.test(oldValue),tip:tip};
            break;
        case "CN"://汉字
            pattern=/^[\u4e00-\u9fa5]{0,}$/;
            tip="only chinese is accept"
            reg=new RegExp(pattern);
            flag={valid:pattern.test(oldValue),tip:tip};
            break;
        case "CNNum"://汉字和数字
            pattern=/^[\u4e00-\u9fa50-9]{0,}$/;
            tip="only chinese or number is accept"
            reg=new RegExp(pattern);
            flag={valid:pattern.test(oldValue),tip:tip};
            break;
        default:
            tip="any input"
            flag={valid:true,tip:tip};
    };
    return flag;
}
const regexp= {reg};
export default regexp;